-module(ehttp_middle_cors).

-export([
    enter/1
    ,leave/1
]).

-include("ehttpd.hrl").


enter(#{req := Req}) ->
    Headers = #{
        <<"Access-Control-Allow-Origin">> => <<"*">>,
        <<"Access-Control-Allow-Methods">> => <<"*">>,
        <<"Access-Control-Allow-Headers">> => <<"*">>,
        <<"Access-Control-Allow-Credentials">> => <<"true">>,
        <<"Access-Control-Max-Age">> => <<"86400">>
    },
    Req2 = cowboy_req:set_resp_headers(Headers, Req),

    case cowboy_req:method(Req) of
        <<"OPTIONS">> ->
            {stop, cowboy_req:reply(200, Req2)};
        _ ->
            {ok, Req2}
    end.

leave(_Req) ->
    ok.
